import requests
import json

api_url = 'http://keliu.xincheng.com:8088/'


def kl_api():
    """
    获取atoken验证码
    """
    url = api_url + "api/v1/user/login"

    payload = json.dumps({
        "appkey": "",
        "username": "huainan",
        "password": "123456"
    })
    headers = {
        'Content-Type': 'application/json'
    }

    api_resp = requests.request("POST", url, headers=headers, data=payload)
    api_resp.content.decode()
    ato_ken = json.loads(api_resp.text)
    ato_ken = ato_ken['atoken']
    return ato_ken


def shop_day_sum(start_day: str, end_day: str):
    """
    店铺天级客流总
    """
    url = api_url + "api/v1/passenger/zoneDay?plaza_unid=9389eb78ba67a9160504d36f32f310ee&startDate=" + start_day + "&endDate=" + str(
        end_day)
    headers = {
        'authorization': kl_api()
    }

    response = requests.request("GET", url, headers=headers)
    response.content.decode()
    data = json.loads(response.text)
    data = data['data']
    kl_sum = []
    for i in data:
        kl_sum.append(i['innum'])
    kl_sum = sum(kl_sum)
    return kl_sum


def travel_sum(start: str, end: str):
    """
    获取指定时间段游逛深度
    """
    shop_data = shop_day_sum(start, end)
    url = api_url + "api/v1/passenger/plazaDay?plaza_unid=9389eb78ba67a9160504d36f32f310ee&startDate=" + start + "&endDate=" + str(
        end)
    headers = {
        'authorization': kl_api()
    }
    resp = requests.get(url, headers=headers)
    resp.content.decode()
    data = json.loads(resp.text)
    data = data['data']
    gc_sum = []
    for i in data:
        gc_sum.append(i['innum'])
    gc_sum = sum(gc_sum)
    data = float(shop_data / gc_sum)
    data = float("%.2f" % data)
    return data
